const Protocol = require("../../Controls/Protocol")
const moment = require("../../miniprogram_npm/moment/index.js")
const util = require("../../utils/util")
import Dialog from '../../miniprogram_npm/vant-weapp/dialog/dialog.js'

function translateTime(time)
{
  const obj = util.getLeftTimeStr(time);

  return obj.h + ":" + obj.m + ":" + obj.s;
}

function refreshPage(that, deltaTime) {
  for (var i = 0; i < that.data.tabitems.length; i++) {
    for (var j = 0; j < that.data.tabitems[i].product.length; j++) {
      const product = that.data.tabitems[i].product[j];
      product.leftTime -= deltaTime;
      product.leftTimeStr = translateTime(product.leftTime);
    }
  }

  if (that.isActive)
  {
    that.setData(that.data);  
  }
}

function reqProduct(that, itemIndex, page, tag, isReload, completeHandler)
{
  Protocol.showProducts(page, tag, {
    success: function (res) {
      if (res.data.code === 0) {
        onReqProductBackSuccess(that, itemIndex, res, isReload);
      } else {
        onReqProductBackFail(that, itemIndex, res, isReload);
      }

    },
    fail: function (res) {
      onReqProductBackFail(that, itemIndex, res, isReload);
    },
    complete:function(res){
      if(completeHandler)
      {
        completeHandler();
      }
    }
  });
}

function onReqProductBackSuccess(that, itemIndex, res, isReload)
{
  const tabItem = that.data.tabitems[itemIndex];
  if (!tabItem)
  {
    return;
  }
  const data = res.data.data;

  if(isReload)
  {
    tabItem.product.length = 0;
  }

  tabItem.currentPage = data.page;
  tabItem.maxPage = data.maxPage;

  for(var i = 0; i < data.products.length; i++)
  {
    const product = data.products[i]

    var left = 0;
    var now = moment();
    var expireTime = moment(product.expireTime);

    if (now < expireTime) {
      left = Math.floor((expireTime - now).valueOf() / 1000);
    }

    if(left <= 0)
    {
      continue;
    }

    const p =
    {
      id: product.id,
      name: product.name,
      images: product.images.split(","),
      count: product.qty,
      leftTime: left,
      leftTimeStr: translateTime(left),
      currentPrice: product.price,
      discountTimes: product.discountTimes,
    }
    tabItem.product.push(p);
  }

  that.setData(that.data);
}

function onReqProductBackFail(that, itemIndex, res)
{

}

function onTimer(that, deltaTime)
{
  refreshPage(that, deltaTime);
}

Page({
  data: {
    currentSelectIndex: 0,
    isLoadingNext: false,
    tabitems:[
      { title: "热门", tag:"hotspot", currentPage:1, maxPage: -1, product: []},
      { title: "其他", tag: "other", currentPage: 1, maxPage: -1, product: []}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.timer = setInterval(() => { onTimer(this, 1) }, 1000)

    const tabItem = this.data.tabitems[this.data.currentSelectIndex];
    if (!tabItem)
    {
      return;
    }

    reqProduct(this, this.data.currentSelectIndex, tabItem.currentPage, tabItem.tag);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.isActive = true;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.isActive = false;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const tabItem = this.data.tabitems[this.data.currentSelectIndex];
    if (!tabItem) {
      return;
    }

    reqProduct(this, this.data.currentSelectIndex, 1, tabItem.tag, true, ()=>{
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("上拉")
    const tabItem = this.data.tabitems[this.data.currentSelectIndex];
    if(!tabItem)
    {
      return;
    }

    if(tabItem.currentPage < tabItem.maxPage)
    {
      reqProduct(this, this.data.currentSelectIndex, tabItem.currentPage+1, tabItem.tag);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onTabChange:function(event){
    this.data.currentSelectIndex = event.detail.index;

    const tabItem = this.data.tabitems[this.data.currentSelectIndex];
    if (!tabItem)
    {
      return;
    }

    if (tabItem.maxPage === -1 && tabItem.tag != "")
    {
      reqProduct(this, this.data.currentSelectIndex, tabItem.currentPage, tabItem.tag);
    }
  },

  onClickitemIvk:function(event)
  {
    const id = event.currentTarget.dataset.item.id;
    wx.navigateTo({ url: `../../pages/product/product?id=${id}` })
  }
})